import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserSettings, saveUserSettings } from '@/utils/storage'

export const useSettingsStore = defineStore('settings', () => {
  // 用户设置
  const settings = ref({
    autoPlay: false,           // 自动播放
    wifiOnly: false,           // 仅 WiFi 下载
    imageQuality: 'standard'  // 图片质量: standard, hd, original
  })
  
  // 初始化设置
  function initSettings() {
    const savedSettings = getUserSettings()
    if (savedSettings) {
      settings.value = { ...settings.value, ...savedSettings }
    }
  }
  
  // 更新设置
  function updateSettings(newSettings) {
    settings.value = { ...settings.value, ...newSettings }
    saveUserSettings(settings.value)
  }
  
  // 获取设置
  function getSettings() {
    return settings.value
  }
  
  // 重置设置
  function resetSettings() {
    settings.value = {
      autoPlay: false,
      wifiOnly: false,
      imageQuality: 'standard'
    }
    saveUserSettings(settings.value)
  }
  
  return {
    settings,
    initSettings,
    updateSettings,
    getSettings,
    resetSettings
  }
})

